<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
class Gass_auth extends CI_Controller {
  function __construct() {
    parent::__construct();
		$this->load->database();
		$this->load->library('session');
 		$this->load->helper('url');
 		$this->load->helper('form');

  	$this->auth = new stdClass;
		$this->load->library('flexi_auth');	
		
    if ($this->flexi_auth->is_logged_in_via_password() && uri_string() != 'gass_auth/logout') {
			if ($this->session->flashdata('message')) { $this->session->keep_flashdata('message'); }
			if ($this->flexi_auth->is_admin()) {
				redirect('gass_news/all_public_news');
			}else{
				redirect('gass_auth_students');
			}
		}
		
		$this->load->vars('base_url', $this->config->item('base_url'));
		$this->load->vars('includes_dir', $this->config->item('includes_dir'));
		$this->load->vars('current_url', $this->uri->uri_to_assoc(1));
		
		$this->data = null;
		$this->output->enable_profiler(TRUE);
	}
	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	
	// 注册、登录部分
	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	
	/**
	 * index
	 * Forwards to 'login'.
	 */ 
  function index()
  {
		$this->login();
  }

 
	/**
	 * login
	 * 所有类型的用户都通过这个登录页面进入系统
	 * 没有账户的用户可以通过邮箱验证，注册一个新的账户
	 * 注意: 这个部分只适合尚未登录的用户使用，如果已经登录的用户访问这个页面，将会被跳转。
	 */ 
    function login()
    {
        if ($this->input->post('login_user'))
        {
			$this->load->model('gass_auth_model');
			$this->gass_auth_model->login();
		}			
		// CAPTCHA Example
		// Check whether there are any existing failed login attempts from the users ip address and whether those attempts have exceeded the defined threshold limit.
		// If the user has exceeded the limit, generate a 'CAPTCHA' that the user must additionally complete when next attempting to login.
        if ($this->flexi_auth->ip_login_attempts_exceeded())
        {

            $js = "document.getElementById('captcha').src='gass_auth/g_captcha/'+Math.random();document.getElementById('captcha-form').focus();";
            $this->data['captcha'] = '<label>验证码</label><img onclick="'.$js.'" title="点击图片更换下一张！" src="gass_auth/g_captcha" id="captcha" /><input type="text" name="captcha" id="captcha-form" autocomplete="off" />'; 

            /******************利用CI自带的captcha辅助函数实现图片验证码*******************************************
            $this->load->helper('captcha');
            $vals = array(
                //'word' => 'abcdefg',
                'expiration' => 60,
                //'font_path' => './includes/resources/fonts/Candice.ttf',
                'img_path' => './captcha/',
                'img_url' => 'http://'.$_SERVER['SERVER_ADDR'].'/gass/captcha/'
            );
            $cap = create_captcha($vals);
            $this->data['captcha'] = '<label>验证码</label>'.$cap['image'].'<input type="text" name="captcha" id="captcha-form" autocomplete="off" />'; 
            //将captcha加入CI的session中
            $this->session->set_userdata('captcha',$cap['word']);
            *********************************************************************************************************/
        }
		//设置产生的提示信息。
		$this->data['message'] = (! isset($this->data['message'])) ? $this->session->flashdata('message') : $this->data['message'];		
        // 对系统报错信息（p标签包裹的数据）进行重新处理；        
        if (!empty($this->data['message']))
        {
            $this->data['alert_message'] = $this->gass_global_model->get_alert_message($this->data['message']);   
        } 
		$this->load->view('user_login_view', $this->data);
  }

	/**
	 * register_account
	 * 所有的新用户都可以在这个页面进行注册
	 * 注意: 这个部分只适合尚未登录的用户使用，如果已经登录的用户访问这个页面，将会被跳转。
	 */ 
    function register_account()
    {
		if ($this->flexi_auth->is_logged_in()) {
			redirect('gass_auth');
		}else if ($this->input->post('register_user')){			
			$this->load->model('gass_auth_model');
			$this->gass_auth_model->register_account();
		}
		$this->session->set_flashdata('message', $this->flexi_auth->get_messages());
		$this->data['message'] = (! isset($this->data['message'])) ? $this->session->flashdata('message') : $this->data['message'];		
        // 对系统报错信息（p标签包裹的数据）进行重新处理；        
          if (!empty($this->data['message']))
          {
              $this->data['alert_message'] = $this->gass_global_model->get_alert_message($this->data['message']);   
          } 
        $this->load->view('user_register_view', $this->data);
	}

	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	
	// 账户激活
	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	

	/**
	 * activate_account
	 * 通过email激活用户账户
	 * 用户通过系统邮件中的一个激活的链接来调用此方法
	 */ 
	function activate_account($user_id, $token = FALSE){
		$this->flexi_auth->activate_user($user_id, $token, TRUE);
		$this->session->set_flashdata('message', $this->flexi_auth->get_messages());
    redirect('gass_auth');
	}	
	
	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	
	// 忘记密码
	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	
	
	/**
	 * forgotten_password_ajax
	 * 通过AJAX来调用该方法，去调用MODEL中的方法发送验证邮件。
	 *
	 */ 
	function forgotten_password_ajax(){
	  $this->load->model('gass_auth_model');
	  $this->gass_auth_model->forgotten_password_ajax($this->input->post('userIdentity'));
		// Get any status message that may have been set.
		$this->data['message'] = (! isset($this->data['message'])) ? $this->session->flashdata('message') : $this->data['message'];		
		$this->load->view('forgot_password_view', $this->data);		
    }
    /**
	 * forgotten_password
	 * 调用MODEL中的方法发送验证邮件。
	 *
	 */ 
    function forgotten_password()
    {
        if ($this->input->post('send_forgotten_password')) 
        {
	  $this->load->model('gass_auth_model');
	  $this->gass_auth_model->forgotten_password($this->input->post('send_forgotten_password'));
		}			
		// Get any status message that may have been set.
		$this->data['message'] = (! isset($this->data['message'])) ? $this->session->flashdata('message') : $this->data['message'];	
          // 对系统报错信息（p标签包裹的数据）进行重新处理；        
          if (!empty($this->data['message']))
          {
              $this->data['alert_message'] = $this->gass_global_model->get_alert_message($this->data['message']);   
          } 
		$this->load->view('user_forgot_pass_view', $this->data);		
	}	
	/**
	 * manual_reset_forgotten_password
	 * This is step 2 (The last step) of an example of allowing users to reset a forgotten password manually. 
	 * See the auto_reset_forgotten_password() function below for an example of directly emailing the user a new randomised password.
	 * In this demo, this page is accessed via a link in the 'views/includes/email/forgot_password.tpl.php' email template, which must be set to 'auth/manual_reset_forgotten_password/...'.
	 */
	function manual_reset_forgotten_password($user_id = FALSE, $token = FALSE){
		// If the 'Change Forgotten Password' form has been submitted, then update the users password.
		if ($this->input->post('change_forgotten_password')) 
		{
			$this->load->model('gass_auth_model');
			$this->gass_auth_model->manual_reset_forgotten_password($user_id, $token);
		}
		// Get any status message that may have been set.
		$this->data['message'] = (! isset($this->data['message'])) ? $this->session->flashdata('message') : $this->data['message'];		
		$this->load->view('reset_pass_view', $this->data);
    }

	/**
	 * g_captcha
	 * 利用google的captcha，创建图片验证码
     */
    function g_captcha()
    {
        $this->load->library('g_captcha');
        $this->g_captcha->CreateImage(); 
    }
		
	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	
	// 退出登录
	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	

	/**
	 * logout
	 * This example logs the user out of all sessions on all computers they may be logged into.
	 * In this demo, this page is accessed via a link on the demo header once a user is logged in.
	 */
	function logout() {
		$this->flexi_auth->logout(TRUE);
		$this->session->set_flashdata('message', $this->flexi_auth->get_messages());		
    redirect('gass_auth');
  }
	
	/**
	 * logout_session
	 * This example logs the user only out of their CURRENT browser session (e.g. Internet Cafe), but no other logged in sessions (e.g. Home and Work).
	 * In this demo, this controller method is actually not linked to. It is included here as an example of logging a user out of only their current session.
	 */
	function logout_session() {
		$this->flexi_auth->logout(FALSE);
    $this->session->set_flashdata('message', $this->flexi_auth->get_messages());		
    redirect('gass_auth');
  }	
}

/* End of file gass_auth.php */
/* Location: ./application/controllers/gass_auth.php */
